package moni.anyou.com.view.utils;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseAppAdapter<T> extends BaseAdapter {

    private ListView listView;
    private GridView gridView;
    private List<T> datas=new ArrayList<>();
    private int resLayout;
    private LayoutInflater inflater;

//    public BaseAppAdapter(ListView listView) {
//        this.listView = listView;
//        inflater=LayoutInflater.from(listView.getContext());
//        listView.setAdapter(this);
//    }


    public BaseAppAdapter(ListView listView, int resLayout, List<T> list) {
        this.listView=listView;
        inflater=LayoutInflater.from(listView.getContext());
        this.datas=list;
        this.resLayout=resLayout;
        listView.setAdapter(this);
    }

    public BaseAppAdapter(ListView listView, int resLayout) {
        this.listView=listView;
        inflater=LayoutInflater.from(listView.getContext());
        this.resLayout=resLayout;
        listView.setAdapter(this);
    }


    public BaseAppAdapter(GridView gridView, int resLayout, List<T> list) {
        this.gridView=gridView;
        inflater=LayoutInflater.from(gridView.getContext());
        this.datas=list;
        this.resLayout=resLayout;
        gridView.setAdapter(this);
    }

    public BaseAppAdapter(GridView gridView, int resLayout) {
        this.gridView=gridView;
        inflater=LayoutInflater.from(gridView.getContext());
        this.resLayout=resLayout;
        gridView.setAdapter(this);
    }

    @Override
    public int getCount(){
       return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        if(convertView==null) {
            convertView=inflater.inflate(resLayout, null);
        }
        return getViewIndex(datas.get(position),position,convertView,parent);
    };

    public abstract View getViewIndex(T data, int position, View convertView, ViewGroup parent);
}
